﻿@{
    
    //    int curr_page = startindex / maxelementperpage;
    int curr_page = 0;
    int maxelementperpage = 10;
    if (UrlData.Count() > 0)
    {
        curr_page = int.Parse(UrlData[0]);
    }
    int startindex = curr_page * maxelementperpage;

    var dbcontext = new alguillet_ExpenseManagerEntities();
    var fulllist = dbcontext.Operations.Where(Op => Op.IDFMember == WebSecurity.CurrentUserId).OrderByDescending(Op => Op.Date).ToList();
    var count_all_items = fulllist.Count();
    List<Operation> list = null;
    var nb_elements = (count_all_items - startindex) > maxelementperpage ? maxelementperpage : (count_all_items - startindex);
    if (nb_elements > 0)
    {
        list = fulllist.GetRange(startindex, nb_elements);
    }
    else
    {
        list = new List<Operation>();
    }

    double sum = fulllist.Sum(Op => Op.Value);
    
}
@if (count_all_items > 0)
{
    <text>Total accounts : @sum
    <table class="datatable">
        <tr>
            <th>
                Date
            </th>
            <th>
                Title
            </th>
            <th>
                Value
            </th>
            <th>
                Reason
            </th>
            <th>
            </th>
        </tr>
        @{
    int i = 0;
    foreach (var item in list)//Here the list is loaded fully
    {
            <tr @if (i++ % 2 == 0)
                {
                <text>class="row1"</text>}
                else
                {<text>class="row2"</text>}>
                <td>
                    @item.Date
                </td>
                <td>
                    @item.Title
                </td>
                <td>
                    @item.Value
                </td>
                <td>
                    @item.Reason
                </td>
                <td>
                    <span data-id='@item.IDOperation' class='deleteoperationbutton' >X</span>
                </td>
            </tr>
    }
        }
        @if (count_all_items > 10)
        {
            <tr>
                <td colspan="5">
                    @if (curr_page > 0)
                    {
                        <span class='PreviousPage'>Previous</span>
                    }
                    @if (nb_elements == maxelementperpage)
                    {
                        <span class='NextPage'>Next</span> 
                    }
                    <span style="float: right;">Page @curr_page</span>
                </td>
            </tr>
        }
    </table>
    </text>
}
else
{
    <text><p>
        You don't have any operation.</p></text>
}